namespace Isolation.Outlook
{
	#region #using Directives

	using System;
	using System.Runtime.InteropServices;

	using Microsoft.Office.Interop.Outlook;

	#endregion

	public class OutlookSession : IOutlookSession
	{
		private Application outlookApp;
		private NameSpace outlookSession;

		#region Implementation of IDisposable

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		private void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this.outlookSession != null)
				{
					Marshal.ReleaseComObject(this.outlookSession);
					this.outlookSession = null;
				}
				if (this.outlookApp != null)
				{
					Marshal.ReleaseComObject(this.outlookApp);
					this.outlookApp = null;
				}
			}
		}

		#endregion

		public OutlookSession()
		{
			try
			{
				this.outlookApp = new Application();

				this.outlookSession = this.outlookApp.Session;
			}
			catch (COMException e)
			{
				throw new OutlookIsolationException(e);
			}
		}

		#region IOutlookSession Members

		public IRecipient CreateRecipient(string recipientName)
		{
			return new Recipient(this.outlookSession.CreateRecipient(recipientName));
		}

		#endregion
	}
}